import {
	sendAjaxRequest,
	findTargetData,
	copy
} from '../libs/utils.js';
import handleDataProcess from '../libs/handleDataProcess.js'
export default {
	methods: {
		initBanner(banner){
			if (banner.staticData && banner.staticData.data) {
				var newData = findTargetData(banner.staticData.data, banner.staticData.bindData || [], 'list');
				// #ifdef H5
				if(this.$editMode){
					newData = copy(newData)
				}
				// #endif
				newData = handleDataProcess(newData, banner.staticData.process,'list');
				// 修复修改组件配置项时,静态数据列表样式不刷新的问题
				this.$nextTick(() => {
					banner.listArray = newData;
					banner.bindData = banner.staticData.bindData || [];
				})
			} else if (banner.ajax && banner.ajax.length > 0) {
				this.sendBannerAjax(banner, banner.ajax[0]);
			}
			// 接收广播,由外部触发,发ajax请求
			var itemEventName = 'sendAjax-' + this.pageConfig.cid + '-' + banner.cid;
			uni.$off(itemEventName)
			uni.$on(itemEventName,payload=>{
				if(banner.ajax && banner.ajax.length > 0){
					this.sendBannerAjax(banner, banner.ajax[0], payload.notToLogin);
				}
			})
			var dotsStyles = {
				width: banner.style.indicatorWidth,
				height: banner.style.indicatorHeight,
				align: banner.style.indicatorsAlignment,
				bottom: banner.style.indicatorBottom,
				left: banner.style.indicatorLeft,
				right: banner.style.indicatorRight,
				color: banner.style.indicatorTextColor,
				selectedColor: banner.style.selectedIndicatorTextColor,
				backgroundColor: banner.style.fillColor,
				border: '1px ' + banner.style.strokeColor + ' solid',
				selectedBackgroundColor: banner.style.selectFillColor,
				selectedBorder: '1px ' + banner.style.selectStrokeColor + ' solid'
			}
			this.$set(banner,'dotsStyles',dotsStyles)
		},
		sendBannerAjax(banner, ajax, notToLogin) {
			sendAjaxRequest(ajax, this.options, '', this.pageConfig.variable, this.pageConfig.cid, this.pageConfig, notToLogin).then(res => {
				var newData = res.listData;
				newData = handleDataProcess(newData, ajax.process,'list');
				banner.listArray = newData;
				banner.bindData = res.bindData;
				// #ifdef H5
				// 修复h5端轮播图数据刷新为1条时,轮播往左偏移导致看不见的问题
				if(this.$editMode){
					setTimeout(()=>{
						var swipers = this.$refs.swiper;
						if(swipers){
							swipers.forEach(swiper=>{
								swiper.$refs.slideFrame.style.transform = 'translate(0%, 0px) translateZ(0px)';
							})
						}
					})
				}
				// #endif
			});
		},
		swiperChange(e,banner){
			if(e.detail.source === 'autoplay' || e.detail.source === 'touch'){
				banner.current = e.detail.current;
			}
		}
	}
}
